package com.sky.service.admin.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.admin.DishFlavorMapper;
import com.sky.mapper.admin.DishMapper;
import com.sky.mapper.admin.SetmealDishMapper;
import com.sky.pojo.Dish;
import com.sky.pojo.DishFlavor;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.admin.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 新增菜品
     * @param dishDTO
     */
    @Override
    public void save(DishDTO dishDTO) {
        Dish dish=new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dish.setCreateTime(LocalDateTime.now());
        dish.setCreateUser(BaseContext.getCurrentId());
        dish.setUpdateTime(LocalDateTime.now());
        dish.setUpdateUser(BaseContext.getCurrentId());

        //插入菜品表
        dishMapper.insert(dish);
        Long id = dish.getId();

        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (!CollectionUtils.isEmpty(flavors)){
            //不为空,代表新增的时候加了口味,需要插入
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(id);

            }
            //向口味表插入数据
            dishFlavorMapper.insertBatch(flavors);
        }

    }

    /**
     * 菜品分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());

        Page<DishVO> page=dishMapper.pageQuery(dishPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }


    /**
     * 删除菜品
     * @param ids
     */
    @Transactional
    @Override
    public void deleteBatch(List<Integer> ids) {
        //起售中的菜品不能删除
            //查询要删除的菜品是否是起售
        for (Integer id : ids) {
           Dish dish= dishMapper.getById(id);
           if (dish.getStatus()==StatusConstant.ENABLE){
               throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
           }
        }



        //被套餐关联的菜品不能删除
        Integer count= setmealDishMapper.selectCountByDishIds(ids);
        if(count>0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        //删除口味表
        dishFlavorMapper.deleteByDishIds(ids);

        //删除菜品

        dishMapper.deleteByIds(ids);
    }


    /**
     * 根据id查询菜品
     * @param dishId
     * @return
     */
    @Override
    public DishVO getDishById(Long dishId) {

        //查询菜品表
         Dish dish=dishMapper.getDishById(dishId);
        //查询菜品口味表
        List<DishFlavor> dishFlavors=dishFlavorMapper.getFlavorById(dishId);

        DishVO dishVO=new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavors);

        return dishVO;

    }

    /**
     * 更新菜品信息
     * @param dishDTO
     */
    @Override
    public void update(DishDTO dishDTO) {
        Dish dish=new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dish.setUpdateUser(BaseContext.getCurrentId());
        dish.setUpdateTime(LocalDateTime.now());


        //检查是否更新了口味,如果更新了,先删除口味菜品表,再重新插入
        List<DishFlavor> dishFlavors = dishDTO.getFlavors();

        if (!CollectionUtils.isEmpty(dishFlavors)){
            //删除口味表
            dishFlavorMapper.deleteByDishId(dish.getId());
            for (DishFlavor dishFlavor : dishFlavors) {
                dishFlavor.setDishId(dish.getId());
            }
            //插入菜品口味表
            dishFlavorMapper.insertBatch(dishFlavors);

        }

        //口味没有变动,直接修改

        dishMapper.update(dish);
    }


    /**
     * 菜品起售停售
     * @param status
     * @param id
     */
    @Override
    public void updateDishStatus(Integer status, Long id) {
        dishFlavorMapper.updateDishStatus(status,id);
    }


    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @Override
    public List<Dish> list(Long categoryId) {

        Dish dish = Dish.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();
        return dishMapper.list(dish);

    }


    /**
     * 根据分类id查询菜品以及口味
     * @param categoryId
     * @return
     */
    @Override
    public List<DishVO> listWithFlavor(Integer categoryId) {
        //先根据分类id查询所有菜品
        List<Dish> dishList = dishMapper.getDishByCategoryId(categoryId);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            //逐个遍历查询到的菜品,遍历到一个,就新建一个dishVO,将遍历到的放进去
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getFlavorByDishId(d.getId());

            //将口味放到dishVO里面
            dishVO.setFlavors(flavors);

            dishVOList.add(dishVO);
        }

        return dishVOList;
    }

}
